package process;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import annotation.Caption;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import util.PathUitls;
import util.StringUilts;

/**
 * version 1.0
 * @author kaizen
 *
 */
@Deprecated
public class Process {
	
	 private static final String prefix="${bean.";
	 private static final String suffix="}";
	 private static final String jspFilePrefix="/jsp/";
	 private static final String ftlFilePrefix="/templetes/";
	 private static final String ENTER = "\n";//换行

   //����һ�����ʵ�Configration���� 
     protected static Configuration configuration = new Configuration();
     
     static{
    	 
	     try 
	     {
			configuration.setDirectoryForTemplateLoading(new File(PathUitls.getClasspath()));
			configuration.setObjectWrapper(new DefaultObjectWrapper());  
	        configuration.setDefaultEncoding("UTF-8");  
	        
	     } catch (IOException e) {
	    	 // TODO Auto-generated catch block
	    	 e.printStackTrace();
	     }  
        
     }

	
	
	
	public static void processJsp(Object obj,String path,String tableCaption) throws IOException{
		if(obj==null||path==null){
			throw new NullPointerException();
		}
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
		Writer writer = null;
		Writer writer2 = null;
		Writer writer3 = null;
		
		 try {  
	            
	            Template template = configuration.getTemplate(jspFilePrefix+"input.jsp");
	            Template template2 = configuration.getTemplate(jspFilePrefix+"list.jsp");
	            Template template3 = configuration.getTemplate(jspFilePrefix+"view.jsp");
	            
	            Map<String, Object> inputParamMap = new HashMap<String, Object>();
	            Map<String, Object> listParamMap = new HashMap<String, Object>();
	           

	            
	            Map<String,Object> captions = new  HashMap<String, Object>();
	            Map<String,Object> listCaptions = new  HashMap<String, Object>();
	            
	            //List fields = new ArrayList();
	            Map<String, Object> fields = new HashMap<String, Object>();
	            
	            Field[] fs =  obj.getClass().getDeclaredFields();
	            for (Field f : fs) {
	            	Caption caption = f.getAnnotation(Caption.class);
	            	if(null!=caption){
	            		if(f.getName().equals("seqId")){
		            		inputParamMap.put("seqId", f.getName());
		            		inputParamMap.put("seqIdValue",prefix+f.getName()+suffix);
		            	}else{
		            		captions.put(f.getName(),caption.value());
		            	}
	            		fields.put( f.getName(),prefix+f.getName()+suffix);
	            		listCaptions.put(prefix+f.getName()+suffix, caption.value());
	            	}
				}
	            //Input
	            inputParamMap.put("fields", fields); 
	            inputParamMap.put("captions", captions); 
	            inputParamMap.put("lowercaseBean", StringUilts.initLowerCase(obj.getClass().getSimpleName()));
	            inputParamMap.put("path","${path}");
	            
	            //List
	            listParamMap.put("listCaptions", listCaptions); 
	            listParamMap.put("page", "${page.results}"); 
	            listParamMap.put("fields", fields);
	            listParamMap.put("listName", tableCaption);
	            listParamMap.put("pageNumber", "${page.pageNumber}");
	            listParamMap.put("totalPages", "${page.totalPages}");
	            listParamMap.put("search","Like"); 
	            listParamMap.put("path","${path}");
	            listParamMap.put("lowercaseBean", StringUilts.initLowerCase(obj.getClass().getSimpleName()));
	            //File file = new File("");
	            //FileOutputStream fos = new FileOutputStream(file);
	            writer = new OutputStreamWriter(new FileOutputStream(path+ StringUilts.initLowerCase(obj.getClass().getSimpleName())+"Input.jsp"),"UTF-8");
	            writer2 = new OutputStreamWriter(new FileOutputStream(path+ StringUilts.initLowerCase(obj.getClass().getSimpleName())+"List.jsp"),"UTF-8");
	            writer3 = new OutputStreamWriter(new FileOutputStream(path+ StringUilts.initLowerCase(obj.getClass().getSimpleName()+"View.jsp")),"UTF-8");

	            template.process(inputParamMap, writer); 
	            template2.process(listParamMap, writer2);
	            template3.process(inputParamMap, writer3); 
	            
	            
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        } catch (TemplateException e) {  
	            e.printStackTrace();  
	        }finally{
	        	if(writer!=null){
	        		writer.close();
	        	}
	        	if(writer2!=null){
	        		writer.close();
	        	}
	        	if(writer3!=null){
	        		writer.close();
	        	}
	        	
	        }
	}
	
	public static void processDao(String packgerOutPath,String beanName,String path){
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
        try 
        {
			Template template = configuration.getTemplate(ftlFilePrefix+"dao.ftl");
           
			Map<String, Object> ParamMap = new HashMap<String, Object>();
            ParamMap.put("packageOutPath", packgerOutPath);
            ParamMap.put("bean", beanName);

            Writer writer  = new OutputStreamWriter(new FileOutputStream(path+"I"+beanName+"Dao.java"),"UTF-8"); 
            template.process(ParamMap, writer);  


			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void processService(String packgerOutPath,String beanName,String path){
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
		//生成Service
        try 
        {
			Template template = configuration.getTemplate(ftlFilePrefix+"iservice.ftl");
           
			Map<String, Object> ParamMap = new HashMap<String, Object>();
            ParamMap.put("packageOutPath", packgerOutPath);
            ParamMap.put("bean", beanName);

            Writer writer  = new OutputStreamWriter(new FileOutputStream(path+"I"+beanName+"Service.java"),"UTF-8"); 
            template.process(ParamMap, writer);  

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void processMService(String packgerOutPath,String beanName,String path,Object obj){
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
		try 
        {
			Template template = configuration.getTemplate(ftlFilePrefix+"mservice.ftl");
			Map<String, Object> ParamMap = new HashMap<String, Object>();
            Map<String,Object> listCaptions = new  HashMap<String, Object>();

			
			Map<String, Object> fields = new HashMap<String, Object>();
			 Field[] fs =  obj.getClass().getDeclaredFields();
	            for (Field f : fs) {
	            	Caption caption = f.getAnnotation(Caption.class);
	            	fields.put( f.getName(), StringUilts.getFisrtUpperCaseChar(f.getName()));
	            	if(null!=caption){
	            		listCaptions.put(f.getName(), caption.value());
	            	}else{
	            		listCaptions.put(f.getName(),"");
	            	}
				}
            ParamMap.put("packageOutPath", packgerOutPath);
            ParamMap.put("bean", beanName);
            ParamMap.put("fields", fields);
            ParamMap.put("listCaptions", listCaptions);
            System.out.println(fields.toString());
            System.out.println(listCaptions.toString());
            Writer writer  = new OutputStreamWriter(new FileOutputStream(path+"M"+beanName+"Service.java"),"UTF-8"); 
            template.process(ParamMap, writer);  

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void processServiceImpl(String packgerOutPath,String beanName,String path){
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
		try 
        {
			Template template = configuration.getTemplate(ftlFilePrefix+"service.ftl");
           
			Map<String, Object> ParamMap = new HashMap<String, Object>();
            ParamMap.put("packageOutPath", packgerOutPath);
            ParamMap.put("bean", beanName);

            Writer writer  = new OutputStreamWriter(new FileOutputStream(path+beanName+"Service.java"),"UTF-8"); 
            template.process(ParamMap, writer);  


			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void processController(String packgerOutPath,String beanName,String path){
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
        try 
        {
			Template template = configuration.getTemplate(ftlFilePrefix+"controller.ftl");
			Template template2 = configuration.getTemplate(ftlFilePrefix+"url.ftl");
			
			
			Map<String, Object> ParamMap = new HashMap<String, Object>();
            ParamMap.put("packageOutPath", packgerOutPath);
            ParamMap.put("bean", beanName);
            ParamMap.put("lowercaseBean", StringUilts.initLowerCase(beanName));

            Writer writer  = new OutputStreamWriter(new FileOutputStream(path+beanName+"Controller.java"),"UTF-8"); 
            Writer writer1  = new OutputStreamWriter(new FileOutputStream(path+"URL.java"),"UTF-8"); 

            template.process(ParamMap, writer);  
            template2.process(ParamMap, writer1); 
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void processMController(String packgerOutPath,String beanName,String path){
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
        try 
        {
			Template template = configuration.getTemplate(ftlFilePrefix+"mcontroller.ftl");
			Template template2 = configuration.getTemplate(ftlFilePrefix+"murl.ftl");

			Map<String, Object> ParamMap = new HashMap<String, Object>();
            ParamMap.put("packageOutPath", packgerOutPath);
            ParamMap.put("bean", beanName);
            ParamMap.put("lowercaseBean", StringUilts.initLowerCase(beanName));

            Writer writer  = new OutputStreamWriter(new FileOutputStream(path+"M"+beanName+"Controller.java"),"UTF-8"); 
            Writer writer1  = new OutputStreamWriter(new FileOutputStream(path+"Murl.java"),"UTF-8"); 

            template.process(ParamMap, writer);  
            template2.process(ParamMap, writer1); 

			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	


	/*public static void main(String[] args) {
	  System.out.println(PathUitl.getClasspath());
    System.out.println(PathUitl.getPorjectPath());
    //System.out.println(ClassLoader.getSystemResource("static.html"));
    System.out.println(Bean.class.getSimpleName());//Bean
    System.out.println(Bean.class.getName());//bean.Bean
    
   
    
    Bean bean = new Bean();
	 try {  
          //����һ�����ʵ�Configration���� 
          Configuration configuration = new Configuration();  
          
        
         configuration.setDirectoryForTemplateLoading(new File(PathUitl.getClasspath()));  
          configuration.setObjectWrapper(new DefaultObjectWrapper());  
          configuration.setDefaultEncoding("UTF-8");   //���һ��Ҫ���ã���Ȼ����ɵ�ҳ���� ������  
          //��ȡ�򴴽�һ��ģ�档  
          Template template = configuration.getTemplate(filePrefix+"input.jsp");
          Template template2 = configuration.getTemplate(filePrefix+"list.jsp");
          Template template3 = configuration.getTemplate(filePrefix+"view.jsp");
          
          Map<String, Object> inputParamMap = new HashMap<String, Object>();
          Map<String, Object> listParamMap = new HashMap<String, Object>();
         

          
          Map<String,Object> captions = new  HashMap<String, Object>();
          Map<String,Object> listCaptions = new  HashMap<String, Object>();
          
          //List fields = new ArrayList();
          Map<String, Object> fields = new HashMap<String, Object>();
          
          Field[] fs =  bean.getClass().getDeclaredFields();
          for (Field f : fs) {
          	Caption caption = f.getAnnotation(Caption.class);
          	if(caption.value().equals("����")){
          		inputParamMap.put("seqId", f.getName());
          		inputParamMap.put("seqIdValue",prefix+f.getName()+suffix);
          	}else{
          		captions.put(f.getName(),caption.value());
          	}
          	
          	fields.put( f.getName(),prefix+f.getName()+suffix);
          	listCaptions.put(prefix+f.getName()+suffix, caption.value());
			}
          //Input
          inputParamMap.put("fields", fields);  
          inputParamMap.put("captions", captions); 
          //List
          listParamMap.put("listCaptions", listCaptions); 
          listParamMap.put("page", "page"); 
          listParamMap.put("fields", fields);  
          listParamMap.put("pageNumber", "${page.pageNumber}");
          listParamMap.put("totalPages", "${page.totalPages}");
          listParamMap.put("search", "search"); 
          
          Writer writer  = new OutputStreamWriter(new FileOutputStream(bean.getClass().getSimpleName()+"Input.jsp"),"UTF-8"); 
          Writer writer2 = new OutputStreamWriter(new FileOutputStream(bean.getClass().getSimpleName()+"List.jsp"),"UTF-8");
          Writer writer3 = new OutputStreamWriter(new FileOutputStream(bean.getClass().getSimpleName()+"View.jsp"),"UTF-8");

          template.process(inputParamMap, writer);  
          template2.process(listParamMap, writer2);
          template3.process(inputParamMap, writer3); 
          System.out.println("��ϲ����ɳɹ�~~");  
      } catch (IOException e) {  
          e.printStackTrace();  
      } catch (TemplateException e) {  
          e.printStackTrace();  
      } 
}*/
	
}
